﻿Imports Persistencia
Imports Controle
Public Class frmConfiguracoes

#Region "Enumerações"
    Enum eModo As Short
        eAguardando = False
        eAlterando = True
    End Enum
#End Region

#Region "Variáveis"
    Private mModo As eModo = eModo.eAguardando
    Private objConfiguracoes As New ctrConfiguracoes
    Private objSeguranca As New ctrSeguranca

    Private drConfiguracoes As DataRow

#End Region

#Region "Propriedades"
    Public Property modoAtual() As eModo
        Get
            Return mModo
        End Get
        Set(ByVal Value As eModo)
            mModo = Value
            alteraModopadrao(Value)
            RaiseEvent alteraModo(Value)
        End Set
    End Property
#End Region

#Region "Métodos"

    Protected Shadows Event alteraModo(ByVal eModoAtual As eModo)

    Private Sub alteraModopadrao(ByVal modo As eModo)

        fraParametros.Enabled = modo
        Me.cmdGravar.Enabled = modo

        Me.cmdSair.Enabled = Not modo
    End Sub

    Private Function Salvar(ByVal iLimContatos As Int32, ByVal bSugerirVisita As Boolean) As Boolean
        Dim btSugerirVisita As Byte

        Try
            objConfiguracoes.GravaConfiguracoes(iLimContatos, "LimiteContatos")
            Return objConfiguracoes.GravaConfiguracoes(bSugerirVisita, "SugerirVisita")
        Catch ex As Exception
            Return False
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Function

    Private Sub CarregarConfiguracoes()
        Dim UltimoBkp As Date

        Try
            drConfiguracoes = objConfiguracoes.RetornaConfiguracoes("")

            If Not (drConfiguracoes Is Nothing) Then

                txtLimiteContatos.Text = drConfiguracoes.Item("LimiteContatos").ToString
                chkSugestaoVisita.Checked = Persistencia.perConversao.nuloParaBoleano(drConfiguracoes.Item("SugerirVisita"))
                UltimoBkp = Persistencia.perConversao.nuloParaData(drConfiguracoes.Item("UltimoBkp"))
                If (UltimoBkp <> Date.MinValue) Then
                    lblUltimoBkp.Text = UltimoBkp
                End If

                lblDescServidor.Text = objSeguranca.Servidor
                lblDescBanco.Text = objSeguranca.NomeBanco
                txtDiretorioBkp.Text = objSeguranca.DiretorioBanco

            End If

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)

        End Try

    End Sub
#End Region

    Private Sub cmdGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGravar.Click

        If (Not Salvar(txtLimiteContatos.Text, chkSugestaoVisita.Checked)) Then

            MsgBox("Ferramentas não atualizadas.", MsgBoxStyle.OkOnly, "Aviso")

        End If
        Me.modoAtual = eModo.eAguardando

    End Sub

    Private Sub frmConfiguracoes_alteraModo(ByVal eModoAtual As eModo) Handles Me.alteraModo

    End Sub

    Private Sub frmFerramentas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.modoAtual = eModo.eAguardando
        Call Me.CarregarConfiguracoes()

    End Sub

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        If (Me.modoAtual = eModo.eAlterando) Then
            Me.cmdCancelar.Text = "Alterar"
            Me.modoAtual = eModo.eAguardando
        ElseIf (Me.modoAtual = eModo.eAguardando) Then
            cmdCancelar.Text = "Cancelar"
            Me.modoAtual = eModo.eAlterando
        End If
    End Sub

    Private Sub cmdGerar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGerar.Click

        Try
            If (txtDiretorioBkp.Text <> String.Empty) Then
                If (objSeguranca.GerarBackup(txtDiretorioBkp.Text)) Then
                    MsgBox("Backup gerado com sucesso.", MsgBoxStyle.Exclamation, "Backup")
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdPasta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Try

            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                txtDiretorioBkp.Text = FolderBrowserDialog1.SelectedPath
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdRestaurar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRestaurar.Click
        Dim sBkp As String
        Dim sSenhaSql As String
        Try

            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                sBkp = OpenFileDialog1.FileName
                If (sBkp <> String.Empty) Then
                    If (frmSenhaBd.ShowDialog = Windows.Forms.DialogResult.OK) Then
                        sSenhaSql = frmSenhaBd.SenhaBD
                        If (objSeguranca.RestaurarBackup(sBkp, sSenhaSql)) Then
                            MsgBox("Backup restaurado com sucesso. " & vbCrLf & "Reinicie o sistema para que seja realizada a consistência de dados.")
                            Application.Exit()
                        End If
                    End If
                End If
            End If

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)

        End Try



    End Sub

End Class
